<!DOCTYPE html>
<html>

<head>
    <title>HTML代码编辑器</title>
    <meta charset="utf-8" />
    <style>
        body {
            margin: 0;
            padding: 10px;
            font-family: Arial, sans-serif;
        }

        .toolbar {
            margin-bottom: 10px;
        }

        button {
            padding: 8px 15px;
            margin-right: 5px;
            background: #4CAF50;
            color: white;
            border: none;
            cursor: pointer;
        }

        button:hover {
            background: #45a049;
        }

        .editor-container {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 10px;
            height: 80vh;
        }

        #codeArea {
            width: 100%;
            padding: 10px;
            border: 1px solid #ccc;
            resize: none;
            font-family: 'Consolas', monospace;
        }

        #previewFrame {
            border: 1px solid #ccc;
            background: white;
            width: 100%;
            height: 100%;
        }
    </style>
</head>

<body>
    <div class="toolbar">
        <button onclick="saveFile()">保存</button>
        <input type="file" id="fileInput" hidden />
        <button onclick="openFile()">打开</button>
        <button onclick="runCode()">运行</button>
        <button onclick="showHelp()">帮助</button>
        <button onclick="exitEditor()">退出</button>
    </div>

    <div class="editor-container">
        <textarea id="codeArea" spellcheck="false"></textarea>
        <iframe id="previewFrame"></iframe>
    </div>

    <script>
        function saveFile() {
            const content = document.getElementById('codeArea').value;
            const blob = new Blob([content], { type: 'text/plain' });
            const url = URL.createObjectURL(blob);

            const a = document.createElement('a');
            a.href = url;
            a.download = `code_${Date.now()}.html`;
            a.click();
            URL.revokeObjectURL(url);
        }

        function openFile() {
            const fileInput = document.getElementById('fileInput');
            fileInput.onchange = e => {
                const file = e.target.files[0];
                const reader = new FileReader();
                reader.onload = () => {
                    document.getElementById('codeArea').value = reader.result;
                };
                reader.readAsText(file);
            };
            fileInput.click();
        }

        function runCode() {
            const code = document.getElementById('codeArea').value;
            const preview = document.getElementById('previewFrame');
            preview.srcdoc = code;
        }

        function showHelp() {
            alert("功能说明：\n1. 保存：下载当前代码\n2. 打开：载入本地文件\n3. 运行：预览效果\n4. 退出：关闭窗口");
        }

        function exitEditor() {
            if (confirm("确定要退出吗？")) {
                window.close();
            }
        }
    </script>
</body>

</html>